﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.IO;
using System.Data;
using System.Text;
using System.Collections;

public partial class data_import : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string methodName = Request["method"];
        if (string.IsNullOrEmpty(methodName)) return;
        if (CheckLogined())
        {
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            method.Invoke(this, null);
        }
        else
            Response.AppendHeader("sessionstatus", "out");
    }

    private bool CheckLogined()
    {
        bool res = false;
        if (Session["su_no"] != null)
        {
            try
            {
                int su_no = Convert.ToInt32(Session["su_no"]);
                Session["su_no"] = su_no;
                string su_login = Convert.ToString(Session["su_login"]);
                Session["su_login"] = su_login;
                string su_power = Convert.ToString(Session["su_power"]);
                Session["su_power"] = su_power;
                res = true;
            }
            catch (Exception e)
            {
                res = false;
            }
        }
        return (res);
    }

    /// <summary>
    /// 获取文件名
    /// </summary>
    /// <returns></returns>
    private string GetFileName()
    {
        Random rd = new Random();
        StringBuilder sb = new StringBuilder();
        sb.Append(DateTime.Now.ToString("yyyyMMddHHmmssff"));
        sb.Append(rd.Next(0, 999999).ToString());
        return sb.ToString();
    }

    public void ImportVideo()
    {
        try
        {
            string dirName = Request.QueryString["dir"];

            // 获得程序路径
            string filePath = System.Configuration.ConfigurationManager.AppSettings["Upload"];
            Hashtable extTable = new Hashtable();
            extTable.Add("flash", System.Configuration.ConfigurationManager.AppSettings["flash"]);
            extTable.Add("image", System.Configuration.ConfigurationManager.AppSettings["image"]);

            //找到目标文件对象
            HttpPostedFile uploadFile = Request.Files["file"];

            // 如果有文件, 则保存到一个地址
            if (uploadFile.ContentLength > 0)
            {
                //获取要保存的文件信息
                FileInfo file = new FileInfo(uploadFile.FileName);

                //获得文件扩展名
                string fileNameExt = file.Extension;

                //新文件名称
                string fileName = GetFileName() + fileNameExt;

                string fileExt = Path.GetExtension(fileName).ToLower();

                if (String.IsNullOrEmpty(fileExt) || Array.IndexOf(((String)extTable[dirName]).Split(','), fileExt.Substring(1).ToLower()) == -1)
                    Response.Write(-2);
                else
                {
                    //保存的文件路径
                    string ServerPath = Server.MapPath(filePath + "flash/");
                    if (!Directory.Exists(ServerPath))
                        Directory.CreateDirectory(ServerPath);

                    uploadFile.SaveAs(ServerPath + fileName);
                    Response.Write(filePath + "flash/" + fileName);
                }
            }
        }
        catch (Exception e)
        {
            Response.Write(-1);
        }
    }
}